package com.credithc.fastdfs.monitor.web;

import com.credithc.fastdfs.api.entity.Constants;
import com.credithc.fastdfs.api.entity.SystemArea;
import com.credithc.fastdfs.monitor.dto.ResourceDTO;
import com.credithc.framework.core.utils.JsonUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2015年5月13日
 * @time 下午2:57:20
 */
@Controller
@RequestMapping("/main")
public class MainController {

	private static final Logger logger = LoggerFactory.getLogger(MainController.class);

	/**
	 * cas登录成功跳转到系统首页
	 * @return
	 */
	@RequestMapping("/index")
	public String toIndex(Model model){
		Subject subject = SecurityUtils.getSubject();
		String realname = (String)subject.getPrincipal();
		realname = realname == null ? "" : realname.trim();
		model.addAttribute("realName", realname);
		return "index";
	}

	/**
	 * 获取用户下的菜单和系统，在页面显示出来
	 *
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/menu", produces = "application/json")
	@ResponseBody
	public Map<String, Object> queryMenuSystem(HttpServletRequest request)
			throws UnsupportedEncodingException {
		StringBuffer srtBackUrl = new StringBuffer();
		srtBackUrl.append("http://");
		// 服务器地址
		srtBackUrl.append(request.getServerName());
		srtBackUrl.append(":");
		// 端口号
		srtBackUrl.append(request.getServerPort());
		// 项目名称
		srtBackUrl.append(request.getContextPath());
		SystemArea sys = new SystemArea();
		sys.setSysId("1");
		sys.setSysName("文件系统监控平台");
		sys.setSysFlag(Constants.MONITOR_SYS_AREA);
		sys.setSysDomain(srtBackUrl.toString());
		//String result = oaHttpInteface(menuUrl+"/"+CasUtil.getAccountId()+"/"+Constants.MONITOR_SYS_AREA, new StringBuffer());
//		String redisUrl = "http://uucbeta.credithc.com:8082/redis-interface/resource";
//		String result = oaHttpInteface(redisUrl + "/37a0a81d61c64740af0a1dc1c7e6590d"
//				+ "/sys_monitor" , new StringBuffer());
		//result = java.net.URLDecoder.decode(result, "UTF-8");
		String result = "[\n" +
				"        {\n" +
				"            \"resourceId\": 94,\n" +
				"            \"resourceName\": \"系统监控\",\n" +
				"            \"resourceFlag\": \"monitor\",\n" +
				"            \"resourcePath\": null,\n" +
				"            \"parentId\": 0,\n" +
				"            \"idTree\": \"0.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1432878490000,\n" +
				"            \"updateDate\": 1432878490000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 1,\n" +
				"            \"state\": \"closed\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        },\n" +
				"        {\n" +
				"            \"resourceId\": 95,\n" +
				"            \"resourceName\": \"流量监控\",\n" +
				"            \"resourceFlag\": \"flow_monitor\",\n" +
				"            \"resourcePath\": \"/monitor/netTraffic\",\n" +
				"            \"parentId\": 94,\n" +
				"            \"idTree\": \"0.94.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1432878517000,\n" +
				"            \"updateDate\": 1432828800000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 3,\n" +
				"            \"state\": \"closed\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        },\n" +
				"        {\n" +
				"            \"resourceId\": 96,\n" +
				"            \"resourceName\": \"容量监控\",\n" +
				"            \"resourceFlag\": \"monitro_capacity\",\n" +
				"            \"resourcePath\": \"/monitor/capacity\",\n" +
				"            \"parentId\": 94,\n" +
				"            \"idTree\": \"0.94.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1432878569000,\n" +
				"            \"updateDate\": 1432828800000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 4,\n" +
				"            \"state\": \"closed\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        },\n" +
				"        {\n" +
				"            \"resourceId\": 97,\n" +
				"            \"resourceName\": \"性能监控\",\n" +
				"            \"resourceFlag\": \"monitor_performance\",\n" +
				"            \"resourcePath\": \"/monitor/performance\",\n" +
				"            \"parentId\": 94,\n" +
				"            \"idTree\": \"0.94.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1432881583000,\n" +
				"            \"updateDate\": 1432881583000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 5,\n" +
				"            \"state\": \"closed\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        },\n" +
				"        {\n" +
				"            \"resourceId\": 98,\n" +
				"            \"resourceName\": \"使用量监控\",\n" +
				"            \"resourceFlag\": \"monirot_capacityline\",\n" +
				"            \"resourcePath\": \"/monitor/capacityLine\",\n" +
				"            \"parentId\": 94,\n" +
				"            \"idTree\": \"0.94.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1432881646000,\n" +
				"            \"updateDate\": 1432881646000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 6,\n" +
				"            \"state\": \"closed\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        },\n" +
				"        {\n" +
				"            \"resourceId\": 99,\n" +
				"            \"resourceName\": \"基本信息\",\n" +
				"            \"resourceFlag\": \"monitor_base\",\n" +
				"            \"resourcePath\": \"/monitor/baseInfo\",\n" +
				"            \"parentId\": 94,\n" +
				"            \"idTree\": \"0.94.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1432881693000,\n" +
				"            \"updateDate\": 1432881693000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 2,\n" +
				"            \"state\": \"open\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        },\n" +
				"        {\n" +
				"            \"resourceId\": 101,\n" +
				"            \"resourceName\": \"上传下载\",\n" +
				"            \"resourceFlag\": \"up_down\",\n" +
				"            \"resourcePath\": null,\n" +
				"            \"parentId\": 0,\n" +
				"            \"idTree\": \"0.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1432881803000,\n" +
				"            \"updateDate\": 1432881803000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 7,\n" +
				"            \"state\": \"closed\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        },\n" +
				"        {\n" +
				"            \"resourceId\": 102,\n" +
				"            \"resourceName\": \"文件管理\",\n" +
				"            \"resourceFlag\": \"file_manage\",\n" +
				"            \"resourcePath\": \"/filemanage/listFileInfo\",\n" +
				"            \"parentId\": 101,\n" +
				"            \"idTree\": \"0.101.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1432881831000,\n" +
				"            \"updateDate\": 1432881831000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 8,\n" +
				"            \"state\": \"closed\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        },\n" +
				"        {\n" +
				"            \"resourceId\": 103,\n" +
				"            \"resourceName\": \"参数管理\",\n" +
				"            \"resourceFlag\": \"param\",\n" +
				"            \"resourcePath\": null,\n" +
				"            \"parentId\": 0,\n" +
				"            \"idTree\": \"0.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1432881932000,\n" +
				"            \"updateDate\": 1432881932000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 9,\n" +
				"            \"state\": \"closed\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        },\n" +
				"        {\n" +
				"            \"resourceId\": 104,\n" +
				"            \"resourceName\": \"系统参数\",\n" +
				"            \"resourceFlag\": \"param_sys\",\n" +
				"            \"resourcePath\": \"/param/sysParam\",\n" +
				"            \"parentId\": 103,\n" +
				"            \"idTree\": \"0.103.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1432881966000,\n" +
				"            \"updateDate\": 1432881966000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 10,\n" +
				"            \"state\": \"closed\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        },\n" +
				"        {\n" +
				"            \"resourceId\": 105,\n" +
				"            \"resourceName\": \"文件参数\",\n" +
				"            \"resourceFlag\": \"param_file\",\n" +
				"            \"resourcePath\": \"/param/fileParam\",\n" +
				"            \"parentId\": 103,\n" +
				"            \"idTree\": \"0.103.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1432882001000,\n" +
				"            \"updateDate\": 1432882001000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 11,\n" +
				"            \"state\": \"closed\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        },\n" +
				"        {\n" +
				"            \"resourceId\": 106,\n" +
				"            \"resourceName\": \"日志管理\",\n" +
				"            \"resourceFlag\": \"log\",\n" +
				"            \"resourcePath\": null,\n" +
				"            \"parentId\": 0,\n" +
				"            \"idTree\": \"0.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1432882031000,\n" +
				"            \"updateDate\": 1432828800000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 12,\n" +
				"            \"state\": \"closed\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        },\n" +
				"        {\n" +
				"            \"resourceId\": 107,\n" +
				"            \"resourceName\": \"错误日志\",\n" +
				"            \"resourceFlag\": \"log_manager\",\n" +
				"            \"resourcePath\": \"/errLogManager/errLogIndex\",\n" +
				"            \"parentId\": 106,\n" +
				"            \"idTree\": \"0.106.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1432882055000,\n" +
				"            \"updateDate\": 1432882055000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 13,\n" +
				"            \"state\": \"closed\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        },\n" +
				"        {\n" +
				"            \"resourceId\": 122,\n" +
				"            \"resourceName\": \"文件数量监控\",\n" +
				"            \"resourceFlag\": \"monitor_filecount\",\n" +
				"            \"resourcePath\": \"/monitor/filecount\",\n" +
				"            \"parentId\": 94,\n" +
				"            \"idTree\": \"0.94.\",\n" +
				"            \"resourceType\": \"1\",\n" +
				"            \"createDate\": 1433477205000,\n" +
				"            \"updateDate\": 1433477205000,\n" +
				"            \"agentName\": \"37a0a81d61c64740af0a1dc1c7e6590d\",\n" +
				"            \"resourceDesc\": null,\n" +
				"            \"sysArea\": \"sys_monitor\",\n" +
				"            \"resourceSort\": 7,\n" +
				"            \"state\": \"closed\",\n" +
				"            \"pkid\": null,\n" +
				"            \"systemName\": null,\n" +
				"            \"opt\": null\n" +
				"        }" +
				"    ]";
		Map<String, Object> map = new HashMap<String, Object>();
		List<ResourceDTO> list = JsonUtils.parseJson(result, List.class,
				ResourceDTO.class);
		List<ResourceDTO> lists = new ArrayList<ResourceDTO>();
		if (list != null && list.size() > 0) {
			for (ResourceDTO dto : list) {
				if ("1".equals(dto.getResourceType())) {
					lists.add(dto);
				}
			}
		}
		map.put("menus", lists);
		map.put("system", sys);
		return map;
	}

	/**
	 * 登出
	 * @author liujiasong
	 * @date 2015年5月20日
	 * @time 上午11:58:03
	 * @return
	 */
	@RequestMapping(value="/logout")
	public ModelAndView logout(){
		Subject currentUser = SecurityUtils.getSubject();
		try {
			currentUser.logout();
			return new ModelAndView(new RedirectView("/login.jsp"));
		} catch (AuthenticationException e) {
			e.printStackTrace();
		}
		return null;
	}
}
